package com.service;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dao.ProductDao;
import com.dao.ShopCarDao;
import com.hibernate.pojo.Product;
import com.hibernate.pojo.Shopcar;

@Service("shopCarService")
public class ShopCarService {

	@Resource(name="shopCarDao")
	private ShopCarDao scd;
	
	@Resource(name="shopcar")
	private Shopcar sc;
	
	@Resource(name="userService")
	private UserServcie us;
	
	@Resource(name="productDao")
	private ProductDao pd;
	
	@Resource(name="product")
	private Product p;
	
	
	public boolean add(double money,int number,int productId,String username){
		sc.setMoney(money*number);
		sc.setNumber(number);
		sc.setProductId(productId);
		p = pd.get(productId);
		if(p.getNumber()<number){
			return false;
		}else{
			sc.setUserId(Integer.parseInt(us.getUserIdByName(username)));
			p.setNumber(p.getNumber()-number);
			return scd.add(sc,p);
		}
	}
	
	public ArrayList<Shopcar> getall(String name){
		return scd.getall(Integer.parseInt(us.getUserIdByName(name)));
	}
}
